from kivyx.lib import runTouchApp, Clock
from kivyx.widgets import Widget, Button, Label
from kivyx.links import Link

from pomodoro import Project, Task, Event


class Pomodoro(Widget):
    def __init__(self, **kwargs):
        Widget.__init__(self, **kwargs)
        self._build()
        self.pomodoro_duration = 25 * 60
        self.shortbreak_duration = 5 * 60
        self.shortbreak_duration = 15 * 60
        self.project = Project()

    def _build(self):
        self.start_btn = Button(text="Start")
        self.einterrupt_btn = Button(text="External\nInterruption", halign="center")
        self.iinterrupt_btn = Button(text="Internal\nInterruption", halign="center")

        Link(self.start_btn, size=((0.5, 0.25), self), align_xy=(Link.TOP, self, Link.CENTER))
        Link(self.einterrupt_btn, size=((0.5, 0.25), self), align_xy=(Link.BOTTOM_LEFT, self))
        Link(self.iinterrupt_btn, size=((0.5, 0.25), self), align_xy=(Link.BOTTOM_RIGHT, self))

        for widget in (self.start_btn, self.einterrupt_btn, self.iinterrupt_btn):
            self.add_widget(widget)

        self.start_btn.bind(on_release=self.start_pomodoro)
        self.einterrupt_btn.bind(on_release=self.mark_interruption)

    def start_pomodoro(self, btn):
        self.current_task.log(Event.start_pomodoro())

    def mark_interruption(self, btn):
        if btn is self.einterrupt_btn:
            self.current_task.log(Event.external_interruption())
        else:
            self.current_task.log(Event.internal_interruption())
        self.enter()



runTouchApp(Pomodoro())

# class Timer(Widget):
#     __events__ = ("on_start", "on_finish")
#     font_size = NumericProperty(12)

#     def __init__(self, **kwargs):
#         Widget.__init__(self, **kwargs)
#         self.label = Label("00:00:000")
#         self.remaining = 0.0


#     def format_seconds()
